﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic;


namespace RomanConverter
{
    class RomanNumeralConverter
    {
        static void Main(string[] args)
        {
            int convertThisNumber;  //Stores value to convert
            string convertedValue;  //Stores RomanNumeral Value
            bool PlayAgain = true; 
            
            //Loop to run until user quits
            while (PlayAgain == true)
            {

                Console.WriteLine("\nEnter a number to convert, or press Q to quit:  \n");

                string inputKey;
                inputKey = Console.ReadLine();
                if (inputKey == "Q" || inputKey == "q") { PlayAgain = false; break; }
                if (Information.IsNumeric(inputKey)) //IsNumeric function from VB library
                {
                    convertThisNumber = Convert.ToInt32(inputKey);
                    convertedValue = ConvertToRoman(convertThisNumber);
                    Console.WriteLine("\n" + convertThisNumber + " written as a roman numeral is: " + convertedValue + "\n" + "\n");
                    convertThisNumber = 0;
                }
                else
                {
                    Console.WriteLine("\nYou typed an invalid entry.");
                }
 
            }
        }

        

        private static string ConvertToRoman(int convertThis)
        {

            int M; int CM; int D; int CD; int C; int XC; int L; int XL; int X; int IX; int V; int IV; int I;

            int leftovers;              //store mod results
            string RomanNumeral = "";   //store roman numeral string

            // 1000 = M
            if (convertThis >= 1000)
            {
                leftovers = convertThis % 1000;
                M = (convertThis - leftovers) / 1000;
                convertThis = leftovers;
                while (M > 0)
                {
                    RomanNumeral += "M"; M--;
                }
            }

            // 900 = CM
            if (convertThis >= 900)
            {
                leftovers = convertThis % 900;
                CM = (convertThis - leftovers) / 900;
                convertThis = leftovers;
                while (CM > 0)
                {
                    RomanNumeral += "CM"; CM--;
                }
            }

            // 500 = D
            if (convertThis >= 500)
            {
                leftovers = convertThis % 500;
                D = (convertThis - leftovers) / 500;
                convertThis = leftovers;
                while (D > 0)
                {
                    RomanNumeral += "D"; D--;
                }
            }

            // 400 = CD
            if (convertThis >= 400)
            {
                leftovers = convertThis % 400;
                CD = (convertThis - leftovers) / 400;
                convertThis = leftovers;
                while (CD > 0)
                {
                    RomanNumeral += "CD"; CD--;
                }
            }

            // 100 = C
            if (convertThis >= 100)
            {
                leftovers = convertThis % 100;
                C = (convertThis - leftovers) / 100;
                convertThis = leftovers;
                while (C > 0)
                {
                    RomanNumeral += "C"; C--;
                }
            }

            // 90 = XC
            if (convertThis >= 90)
            {
                leftovers = convertThis % 90;
                XC = (convertThis - leftovers) / 90;
                convertThis = leftovers;
                while (XC > 0)
                {
                    RomanNumeral += "XC"; XC--;
                }
            }

            // 50 = L
            if (convertThis >= 50)
            {
                leftovers = convertThis % 50;
                L = (convertThis - leftovers) / 50;
                convertThis = leftovers;
                while (L > 0)
                {
                    RomanNumeral += "L"; L--;
                }
            }

            // 40 = XL
            if (convertThis >= 40)
            {
                leftovers = convertThis % 40;
                XL = (convertThis - leftovers) / 40;
                convertThis = leftovers;
                while (XL > 0)
                {
                    RomanNumeral += "XL"; XL--;
                }
            }

            // 10 = X
            if (convertThis >= 10)
            {
                leftovers = convertThis % 10;
                X = (convertThis - leftovers) / 10;
                convertThis = leftovers;
                while (X > 0)
                {
                    RomanNumeral += "X"; X--;
                }
            }

            // 9 = IX
            if (convertThis >= 9)
            {
                leftovers = convertThis % 9;
                IX = (convertThis - leftovers) / 9;
                convertThis = leftovers;
                while (IX > 0)
                {
                    RomanNumeral += "IX"; IX--;
                }
            }

            // 5 = V
            if (convertThis >= 5)
            {
                leftovers = convertThis % 5;
                V = (convertThis - leftovers) / 5;
                convertThis = leftovers;
                while (V > 0)
                {
                    RomanNumeral += "V"; V--;
                }
            }

            // 4 = IV
            if (convertThis >= 4)
            {
                leftovers = convertThis % 4;
                IV = (convertThis - leftovers) / 4;
                convertThis = leftovers;
                while (IV > 0)
                {
                    RomanNumeral += "IV"; IV--;
                }
            }

            // 1 = I
            if (convertThis >= 1)
            {
                leftovers = convertThis % 1;
                I = (convertThis - leftovers) / 1;
                convertThis = leftovers;
                while (I > 0)
                {
                    RomanNumeral += "I"; I--;
                }
            }
            return RomanNumeral;

        }
    }
}
